AddHandlerByName(da, "RowUpdated", New OleDbRowUpdatedEventHandler(AddressOf OnOleDbRowUpdated), New SqlRowUpdatedEventHandler(AddressOf OnSqlRowUpdated))
' Update the data source.
da.Update(ds, "Titles")
cn.Close()
End Sub
' Create a suitable connection object for a given connection string.
Function CreateConnection(ByVal connString As String) As IDbConnection
If connString.ToLower.IndexOf("provider=") >= 0 Then
Return New OleDbConnection(connString)
Else
Return New SqlConnection(connString)
End If
End Function
' Create a suitable DataAdapter object for a given connection object.
Function CreateDataAdapter(ByVal sql As String, ByVal cn As IDbConnection) As DbDataAdapter
If TypeOf cn Is OleDbConnection Then
' Create an OleDbDataAdapter and initialize its properties.
Dim da As New OleDbDataAdapter(sql, DirectCast(cn, OleDbConnection))
Dim cb As New OleDbCommandBuilder(da)
da.UpdateCommand = cb.GetUpdateCommand
da.DeleteCommand = cb.GetDeleteCommand
da.InsertCommand = cb.GetInsertCommand
Return da
ElseIf TypeOf cn Is SqlConnection Then
' Create an SqlDataAdapter and initialize its properties.
Dim da As New SqlDataAdapter(sql, DirectCast(cn, SqlConnection))
Dim cb As New SqlCommandBuilder(da)
da.UpdateCommand = cb.GetUpdateCommand
da.DeleteCommand = cb.GetDeleteCommand
da.InsertCommand = cb.GetInsertCommand
Return da
Else
Throw New ArgumentException()
End If
End Function
' A generic routine that takes an object, an event name, and a list of potential
' delegates to event handlers, and selects the first delegate that matches the
' expected signature of the event handler.
Sub AddHandlerByName(ByVal obj As Object, ByVal eventName As String, ByVal ParamArray events() As [Delegate])
' Get the type of the object argument.
Dim ty As System.Type = obj.GetType
' Get the EventInfo corresponding to the requested event
Dim evInfo As System.Reflection.EventInfo = ty.GetEvent(eventName)
' Get the delegate class that represents the event procedure
Dim evType As System.Type = evInfo.EventHandlerType
' Compare this type with arguments being passed.
Dim del As [Delegate]
For Each del In events
If del.GetType Is evType Then
' If this is the right delegate, add it to the list of event handlers and exit.
evInfo.AddEventHandler(obj, del)
Exit Sub
End If
Next
End Sub
' An example of two event procedure that delegate their job to a common routine.
Sub OnOleDbRowUpdated(ByVal sender As Object, ByVal e As System.Data.OleDb.OleDbRowUpdatedEventArgs)
Debug.WriteLine("OnOleDbRowUpdated")
OnRowUpdated(sender, e)
End Sub
Sub OnSqlRowUpdated(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlRowUpdatedEventArgs)
Debug.WriteLine("SqlDbRowUpdated")
OnRowUpdated(sender, e)
End Sub
Sub OnRowUpdated(ByVal sender As Object, ByVal e As System.Data.Common.RowUpdatedEventArgs)